package com.lbb.simple.demo.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.lbb.simple.databinding.ActivityLoginBinding
import com.lbb.simple.demo.bean.User
import com.lbb.simple.demo.controller.UserController

class LoginActivity : BaseActivity<ActivityLoginBinding>(), TextWatcher, UserController.UserView {
    private val userPresenter = UserController.UserPresenter(this)
    override fun getLayoutContent(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b.materialToolbar.materialToolbar.title = "登录"
        b.textInputEditTextPhone.addTextChangedListener(this)
        b.textInputEditTextPassword.addTextChangedListener(this)
        b.materialButton.isEnabled = false
        b.materialButton.setOnClickListener {
            userPresenter.login(
                b.textInputEditTextPhone.text.toString(),
                b.textInputEditTextPassword.text.toString()
            )
        }
    }

    override fun setUser(user: User?) {

    }

    override fun showTopProgressDialog(s: String?, cancelable: Boolean?) {
        super.showTopProgressDialog(s, cancelable)
        b.materialButton.isEnabled = false
    }

    override fun showCenterProgressDialog(s: String?, cancelable: Boolean?) {
        super.showCenterProgressDialog(s, cancelable)
        b.materialButton.isEnabled = false
    }

    override fun dismissProgressDialog() {
        super.dismissProgressDialog()
        b.materialButton.isEnabled = true
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        b.materialButton.isEnabled = b.textInputEditTextPhone.text.toString()
            .isNotEmpty() && b.textInputEditTextPassword.text.toString().isNotEmpty()
    }
}